home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: Structure names are pointers? Writing to them?
- Date: 21 Mar 1996 16:31:07 GMT
- Organization: Borland International
- Message-ID: <4is08b$in8@druid.borland.com>
- References: <4idmdo$2b2@news.voicenet.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4idmdo$2b2@news.voicenet.com>, deaton@cygnus.rsabbs.com says...
- >
- >
- > I'm using several random access data files in my program(Written in
- TurboC/C++), and all except for one are ARRAYS of STRUCTURES.
- >With these files, I have no syntax problems reading and writing to them.
- >For instance:
- >
- >FILE * fptr;
- >blahS blah[15];
- >
- >...code to populate structure...
- >
- >fptr=fopen("blah.dat","w+");
- >fwrite(blah,sizeof(blah),1,fptr);
- >fclose(fptr);
- >
- >This bit of code works fine, but when I try to write only one structure:
- >
- >FILE * fptr;
- >blahS blah;
- >
- >...code to populate structure...
- >
- >fptr=fopen("blah.dat","w+");
- >fwrite(blah,sizeof(blah),1,fptr);
- >fclose(fptr);
- >
- >my compiler spits out an error. As far as I understood, structure names acted
- the
- >same way as array names: they are pointers. So why isn't this working? I
- really don't
- >want to declare a structure array of 1 element. It would work, but that would
- be pretty lame.
-
- Structure names do not act the same way as array names, and array names are not
- pointers. The name of an array decays into a pointer to its first element in
- most situations, which is why the original code worked. When dealing with a
- single object, however, you must tell the compiler to use its address:
-
- fwrite( &blah,sizeof(blah),1,fptr);
-
-